import {defineStore} from 'pinia';
import {store} from '../index';
import {APP_DARK_MODE_KEY_} from "../../enums/cacheEnum"
import {ThemeEnum} from '../../enums/appEnum';
import {darkMode} from '../../settings';

interface AppState {
    darkMode?: ThemeEnum;
    // Page loading status
    pageLoading: boolean;
    version: string
    // project config
}

export const useAppStore = defineStore({
    id: 'app',
    state: (): AppState => ({
        darkMode: undefined,
        pageLoading: false,
        version: "v0.0.1",
    }),
    getters: {
        getDarkMode(): 'light' | 'dark' | string {
            return this.darkMode || localStorage.getItem(APP_DARK_MODE_KEY_) || darkMode;
        },
        getVersion():string{
            return this.version
        }
    },
    actions: {
        setDarkMode(mode: ThemeEnum): void {
            this.darkMode = mode;
            localStorage.setItem(APP_DARK_MODE_KEY_, mode);
        },
    },

});

// Need to be used outside the setup
export function useAppStoreWithOut() {
    return useAppStore(store);
}
